#! user/bin/python
# -*- coding: utf-8 -*-

"""
Created on 2021/8/23 19:11

@File: 行缓冲_socket-unbuff-client.py
@Author: LT
@Description: Use of this module
"""
import time
from socket import *

HOST = 'localhost'
PORT = 50007

sock = socket(AF_INET, SOCK_STREAM)
sock.connect((HOST, PORT))

# 对makefile采用行缓冲模式
file = sock.makefile("w", buffering=1)                          # file现在是重定向了客户端sock的
# file = sock.makefile("w", buffering=-1)                          # file现在是重定向了客户端sock的
# file = sock.makefile("wb", buffering=0)                          # file现在是重定向了客户端sock的
flush_switch = 0                                                # 设为1来开启手动刷入，0为关闭


# print(f"client now sending data1 to server...")
# file.write("spam\n")
# time.sleep(1)                                                   # 必须后续跟随flush()来正确发送
# if flush_switch:
#     file.flush()
#
#
# print(f"client now sending data2 to server...")
# print("eggs", file=file)                                        # 即使把makefile的file作为print的file参数传入也不能正确输出
# time.sleep(1)
# if flush_switch:
#     file.flush()
#
#
# print(f"client now sending data3 to server...")
# sock.send(b'ham\n')                                             # 如果不flush其他两个的话，这个在server处会是第一个被收到！因为低层级的字节串端口会立即发送
# time.sleep(1)
# if flush_switch:
#     file.flush()


# def test1(file):
#     print(f"client now sending data1 to server...")
#     file.write("spam\n")
#     time.sleep(1)                                                   # 必须后续跟随flush()来正确发送
#     if flush_switch:
#         file.flush()
#
#
# def test2(file):
#     print(f"client now sending data2 to server...")
#     print("eggs", file=file)                                        # 即使把makefile的file作为print的file参数传入也不能正确输出
#     time.sleep(1)
#     if flush_switch:
#         file.flush()
#
#
# def test3(file):
#     print(f"client now sending data3 to server...")
#     sock.send(b'ham\n')                                             # 如果不flush其他两个的话，这个在server处会是第一个被收到！因为低层级的字节串端口会立即发送
#     time.sleep(1)
#     if flush_switch:
#         file.flush()


# def test_all():
#     HOST = 'localhost'
#     PORT = 50007
#
#     sock = socket(AF_INET, SOCK_STREAM)
#     sock.connect((HOST, PORT))
#
#     # 对makefile采用行缓冲模式
#     file = sock.makefile("w", buffering=1)  # file现在是重定向了客户端sock的
#     # file = sock.makefile("w", buffering=-1)                          # file现在是重定向了客户端sock的
#     # file = sock.makefile("wb", buffering=0)                          # file现在是重定向了客户端sock的
#     flush_switch = 0  # 设为1来开启手动刷入，0为关闭
#     print(f"client now sending data1 to server...")
#     file.write("spam\n")
#     time.sleep(1)                                                   # 必须后续跟随flush()来正确发送
#     if flush_switch:
#         file.flush()
#
#     print(f"client now sending data2 to server...")
#     print("eggs", file=file)                                        # 即使把makefile的file作为print的file参数传入也不能正确输出
#     time.sleep(1)
#     if flush_switch:
#         file.flush()
#
#     print(f"client now sending data3 to server...")
#     sock.send(b'ham\n')                                             # 如果不flush其他两个的话，这个在server处会是第一个被收到！因为低层级的字节串端口会立即发送
#     time.sleep(1)
#     if flush_switch:
#         file.flush()


###################
# 结论：
# 即使你标明了行缓冲标志，但协会规定了socket封装文件的写入还是会被缓冲
###################
# if __name__ == '__main__':
#     flush_switch = 0  # 设为1来开启手动刷入，0为关闭
#     test_all()
#     test1(file=file)
#     test2(file=file)
#     test3(file=file)
